package com.ui.design.view.radar.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

/**
 * 思路：我们首先初始化画笔，并且获取到控件的宽高，在onMeasure()中设置铺满，然后在onDraw()方法中绘制四个静态圆和一个渐变圆，
 * 我们通过Matrix矩阵来让他不停的旋转就达到我们想要的效果了
 */

/*
 *created by ZhuYueZong
 * in 2021/7/25
 */


public class RadarView extends View {
    private Paint mPaintLine,mPaintCircle;
    private int w,h;
    //动画
    private Matrix matrix;
    //旋转角度
    private int start;
    //Handler定时动画
    private Handler handler= new Handler();
    private Runnable run = new Runnable() {
        @Override
        public void run() {
            start =start+1;
            matrix = new Matrix(  );
            //参数，旋转角度，围绕点坐标x，y坐标点
            matrix.postRotate( start,w/2,h/2 );
            //刷新重绘
            RadarView.this.invalidate();
            //延时循环
            handler.postDelayed( run,60 );
        }
    };

    public RadarView(Context context) {
        super( context );
    }

    public RadarView(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
        initView();
        //获取长高
        w = context.getResources().getDisplayMetrics().widthPixels;
        h = context.getResources().getDisplayMetrics().heightPixels;
        //一致旋转
        handler.post( run );
    }

    private void initView(){
        mPaintLine = new Paint(  );
        mPaintLine.setColor( Color.WHITE );
        mPaintLine.setAntiAlias( true );
        mPaintLine.setStyle( Paint.Style.STROKE );

        mPaintCircle = new Paint(  );
        mPaintCircle.setColor( Color.RED );
        mPaintCircle.setAntiAlias( true );

        matrix = new Matrix(  );
    }

    @Override
    protected void  onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        setMeasuredDimension( w,h );
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw( canvas );
        //画出四个园,
        /*
        *cx参数表示当前绘制的圆圆心距离左边框的距离
        *cy参数表示当前绘制的圆圆心距离上边框的距离
        * radius表示当前绘制的圆半径大小
         */
        canvas.drawCircle(w / 2, h / 2, w / 2, mPaintLine);
        canvas.drawCircle(w / 2, h / 2, w / 4, mPaintLine);
        canvas.drawCircle(w / 2, h / 2, w / 8, mPaintLine);
        canvas.drawCircle(w / 2, h /2, w / 16, mPaintLine);

        // 绘制渐变圆
        Shader mShader = new SweepGradient(w / 2, h / 2, Color.TRANSPARENT,
                Color.parseColor("#AAAAAAAA"));
        // 绘制时渐变
        mPaintCircle.setShader(mShader);
        // 增加旋转动画，使用矩阵实现
        canvas.concat(matrix); // 前置动画
        canvas.drawCircle(w / 2, h / 2, w * 7 / 10, mPaintCircle);

    }

}
